import { loginByEmail, logout, getInfo } from "api/login";
import Cookies from "js-cookie";
const rolesMap = ["admin", "message", "vote"];
const user = {
  state: {
    user: "",
    status: "",
    email: "",
    code: "",
    uid: undefined,
    auth_type: "",
    token: Cookies.get("token"),
    name: "",
    avatar: "",
    introduction: "",
    roles: [],
    setting: {
      articlePlatform: []
    }
  },

  mutations: {
    SET_AUTH_TYPE: (state, type) => {
      state.auth_type = type;
    },
    SET_CODE: (state, code) => {
      state.code = code;
    },
    SET_TOKEN: (state, token) => {
      state.token = token;
    },
    SET_UID: (state, uid) => {
      state.uid = uid;
    },
    SET_USERNAME: (state, username) => {
      state.username = username;
    },
    SET_INTRODUCTION: (state, introduction) => {
      state.introduction = introduction;
    },
    SET_SETTING: (state, setting) => {
      state.setting = setting;
    },
    SET_STATUS: (state, status) => {
      state.status = status;
    },
    SET_NAME: (state, name) => {
      state.name = name;
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar;
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles;
    },
    LOGIN_SUCCESS: () => {
      console.log("login success");
    },
    LOGOUT_USER: state => {
      state.user = "";
    }
  },

  actions: {
    // 用户名登陆
    LoginByUsername({ commit }, userInfo) {
      const username = userInfo.username.trim();
      return new Promise((resolve, reject) => {
        loginByEmail(username, userInfo.password)
          .then(response => {
            // console.log(response);
            const jsonData = response.data;
            if (jsonData.success) {
              Cookies.set("token", jsonData.data.token);
              commit("SET_TOKEN", jsonData.data.token);

              resolve(jsonData.data.user);
            } else {
              reject(jsonData.message);
            }
          })
          .catch(error => {
            reject(error);
          });
      });
    },

    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo(state.token)
          .then(response => {
            const data = response.data.data;
            // console.log(data);
            commit("SET_ROLES", data.roles);
            commit("SET_USERNAME", data.name);
            commit("SET_AVATAR", data.avatar);
            commit("SET_UID", data.uid);
            commit("SET_INTRODUCTION", data.introduction);
            resolve(response);
          })
          .catch(error => {
            reject(error);
          });
      });
    },

    // 第三方验证登录
    LoginByThirdparty({ commit, state }, code) {
      return new Promise((resolve, reject) => {
        commit("SET_CODE", code);
        loginByThirdparty(
          state.status,
          state.username,
          state.code,
          state.auth_type
        )
          .then(response => {
            commit("SET_TOKEN", response.data.token);
            Cookies.set("Admin-Token", response.data.token);
            resolve();
          })
          .catch(error => {
            reject(error);
          });
      });
    },

    // 登出
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token)
          .then(() => {
            commit("SET_TOKEN", "");
            commit("SET_ROLES", []);
            Cookies.remove("Admin-Token");
            resolve();
          })
          .catch(error => {
            reject(error);
          });
      });
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit("SET_TOKEN", "");
        Cookies.remove("Admin-Token");
        resolve();
      });
    },

    // 动态修改权限
    ChangeRole({ commit }, role) {
      return new Promise(resolve => {
        commit("SET_ROLES", [role]);
        commit("SET_TOKEN", role);
        Cookies.set("Admin-Token", role);
        resolve();
      });
    }
  }
};

export default user;
